package readResults;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import util.GAConfig;
import util.Log;


public class ReadLogFiles {
	
	   public static List<Class<?>>  readClassFromFile() {
		   File logFiles=GAConfig.getLogFiles();
		   File[] listOfFiles = logFiles.listFiles();
	    	List<Class<?>>   classToTest = new LinkedList<Class<?>>();
	    	try{
	    	for (int i = 0; i < listOfFiles.length; i++) {
	    		File file = listOfFiles[i];
	    		String fileName = "untitled folder/"+file.getName().substring(file.getName().lastIndexOf('/')+1);
	    		fileName=fileName.substring(0, fileName.indexOf("_0_"));
	    		Log.logConfig(fileName);
	    		if (!file.isFile() && file.getName().endsWith(".txt")) continue; 
	    		System.out.println("Read tested class from file: "+ file);
				try {
					boolean flag=true;
					BufferedReader br = new BufferedReader(new FileReader(file));
					String line = br.readLine();
					while (line != null) {
						if (line.contains("Total target branchs:")&&flag){
							Log.log(line);
							flag=false;
						}

						if (line.contains("BR Covered SUMMARY")){
							String totalCoverage=line.substring(line.indexOf('(')+1);
							totalCoverage=totalCoverage.substring(0, totalCoverage.indexOf(')'));
//							System.out.println(totalCoverage);
							Log.log(totalCoverage);
						}
						line = br.readLine();
					}
				Log.log.close();	
				}    catch (FileNotFoundException e) {
			          e.printStackTrace();
			          System.exit(0);
		        } catch (IOException e) {
		          e.printStackTrace();
		        }

			}
		} catch (Exception e) {
			System.out.println("Something went wrong: "+ e.getMessage()+"\n");
			System.exit(1);
		}
			return classToTest;
	}

	
	
	
	public  static void main(String args[]) throws ClassNotFoundException,IOException {

		ReadLogFiles.readClassFromFile();
		System.exit(0);
	
	
	}
}
